<?php

class SeoUrlsPlugin extends CorePlugin{
    public function __construct() {
        Yii::app()->plugin->onMemePostUrl = array($this, 'post_url');
        Yii::app()->plugin->onBeforeActions = array($this, 'add_actions');
    }


    public function add_actions($e) {
        if(!$e->sender->isBackend) {
            $e->sender->actions['seo_url'] = array(
                'class' => 'ext.plugins.SeoUrls.actions.SeoUrlAction'
            );
        }
    }
    
    public function post_url($e) {
        
        $slug = preg_replace('/[^\w\p{L}\p{N}\p{Pd}]/u', '-', $e->sender->title);
        $slug = preg_replace('/[_-]+/', '-', $slug);
        
        $e->params['post_url'] = Yii::app()->createAbsoluteUrl('site/seo_url', array('id' => $e->sender->meme_id, 'slug' => trim($slug, '-')));
    }
}